package com.whg.demo.utils;

import com.google.gson.Gson;
import com.whg.demo.model.DmGk;
import com.whg.demo.model.DmJnhyd;
import com.whg.demo.model.DmXzqh;
import com.whg.demo.model.DmJson;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author whg
 * @create 2023/1/10 10:00
 * @description：
 */
public class ReadFileUtil {

    public static void main(String[] args) {
        /*BufferedReader reader;
        try {
            File file = new File("C:\\Users\\whg\\Desktop\\body.txt");
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")));
            String line = reader.readLine();
            reader.close();
            *//*while (line != null) {
                System.out.println(line);
                // read next line
                line = reader.readLine();
            }*//*
            String matchStr = "<div class=\"cell el-tooltip\" style=\"width:";
            String matchStr1 = "px;\">";
            String matchStr2 = "</div>";
            int length = matchStr.length();
            int length1 = matchStr1.length();
            List<String> list = new ArrayList<>();
            while (line.contains(matchStr)){
                int index = line.indexOf(matchStr);
                line = line.substring(index+length);
                int index1 = line.indexOf(matchStr1);
                line = line.substring(index1+length1);
                int index2 = line.indexOf(matchStr2);
                list.add(line.substring(0,index2));
            }
            list.forEach(obj -> System.out.println(obj));
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        //readFile();
        readHtml();
    }

    public static List<String> readFile(){
        List<String> list = new ArrayList<>();
        BufferedReader reader;
        try {
            File file = new File("C:\\Users\\whg\\Desktop\\body.txt");
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")));
            String line = reader.readLine();
            reader.close();
            /*while (line != null) {

                // read next line
                line = reader.readLine();
            }*/
            String matchStr = "<div class=\"cell el-tooltip\" style=\"width: 248px;\">";
            String matchStr1 = "px;\">";
            String matchStr2 = "</div>";
            int length = matchStr.length();
            int length1 = matchStr1.length();
            while (line.contains(matchStr)){
                int index = line.indexOf(matchStr);
                line = line.substring(index+length);
                /*int index1 = line.indexOf(matchStr1);
                line = line.substring(index1+length1);*/
                int index2 = line.indexOf(matchStr2);
                list.add(line.substring(0,index2));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //list.forEach(obj -> System.out.println(obj));
        return list;
    }

    public static List<String> readHtml() {
        List<String> list = new ArrayList<>();
        BufferedReader reader = null;
        File file = new File("D:\\applicationSoft\\selenium-3.14.0\\pythonFiles\\work/work1.html");
        String line = null;
        try{
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")));
            String matchStr = "<div class=\"cell el-tooltip\" style=\"width: 248px;\">";
            String matchStr1 = "px;\">";
            String matchStr2 = "</div>";
            int length = matchStr.length();
            int length1 = matchStr1.length();
            while ((line = reader.readLine()) != null){
                while (line.contains(matchStr)){
                    int index = line.indexOf(matchStr);
                    line = line.substring(index+length);
                    /*int index1 = line.indexOf(matchStr1);
                    line = line.substring(index1+length1);*/
                    int index2 = line.indexOf(matchStr2);
                    list.add(line.substring(0,index2));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public static List<DmXzqh> getXzqhList() {
        List<DmXzqh> list = new ArrayList<>();
        BufferedReader reader = null;
        File file = new File("D:\\applicationSoft\\selenium-3.14.0\\pythonFiles\\work/xzqh.txt");
        String line = null;
        try{
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")));
            Gson gson = new Gson();
            while ((line = reader.readLine()) != null){
                //line = line.replace(": ",":").replace("&amp;", "&").replace("&quot;", "\"");;
                line = line.replaceAll("\"\\{","{")
                        .replaceAll("}\"","}")
                        .replaceAll("\\\\","");
                DmJson text = gson.fromJson(line, DmJson.class);
                if(null != text){
                    //list.addAll(text.getText().getData().getData());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public static List<DmJnhyd> getJnhydList() {
        List<DmJnhyd> list = new ArrayList<>();
        BufferedReader reader = null;
        File file = new File("D:\\applicationSoft\\selenium-3.14.0\\pythonFiles\\work/jnhyd.txt");
        String line = null;
        try{
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")));
            Gson gson = new Gson();
            while ((line = reader.readLine()) != null){
                //line = line.replace(": ",":").replace("&amp;", "&").replace("&quot;", "\"");;
                line = line.replaceAll("\"\\{","{")
                        .replaceAll("}\"","}")
                        .replaceAll("\\\\","");
                DmJson text = gson.fromJson(line, DmJson.class);
                if(null != text){
                    //list.addAll(text.getText().getData().getData());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public static List<DmGk> getGkList() {
        List<DmGk> list = new ArrayList<>();
        BufferedReader reader = null;
        File file = new File("D:\\applicationSoft\\selenium-3.14.0\\pythonFiles\\work/gk.txt");
        String line = null;
        try{
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")));
            Gson gson = new Gson();
            while ((line = reader.readLine()) != null){
                //line = line.replace(": ",":").replace("&amp;", "&").replace("&quot;", "\"");;
                line = line.replaceAll("\"\\{","{")
                        .replaceAll("}\"","}")
                        .replaceAll("\\\\","");
                DmJson text = gson.fromJson(line, DmJson.class);
                if(null != text){
                    list.addAll(text.getText().getData().getData());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }


}
